<!doctype html>
<!--
  ~ Copyright (c) 2005, 2014 vacoor
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  -->
<html class="app">
<head>
    <meta charset="utf-8">
    <title>Terminal - WDB</title>
    <link rel="bookmark" href="favicon.ico"/>
    <link rel="shortcut icon" type="image/x-icon" href="img/terminal.ico"/>
    <link rel="stylesheet" type="text/css" href="css/terminal.css">
    <script src="js/lib/term.js"></script>
    <script src="js/lib/web-socket-js/swfobject.js"></script>
    <script src="js/lib/web-socket-js/web_socket.js"></script>
</head>
<body class="focused">
<div id="loader">...</div>
<script type="text/javascript">
    // <![CDATA[
    (function () {
        // Set URL of your WebSocketMain.swf here:
        WEB_SOCKET_SWF_LOCATION = "WebSocketMain.swf";

        var console = window.console || { log: function () {
        } };
        console.log('use', window.WebSocket);

        var hostname = location.hostname,
            path = /(?:.*(?=\/[^/]*\/))/.exec(location.pathname || '')[0],
            prefix = 'ws://' + hostname + ':8025' + path + '/ws',
            mode = '',// /m=([^?&]+)/.exec(location.search)[1] || '',
            mode = /m=([^?&]+)/.exec(location.search)[1] || '',
            host, port, user, passwd, url,
            autoConnect;

        if ('local' != mode) {
            host = /h=([^?&]+)/.exec(location.search)[1] || '';
            port = /P=([^?&]+)/.exec(location.search)[1] || '22';
            user = /u=([^?&]+)/.exec(location.search)[1] || '';
            passwd = /p=([^?&]+)/.exec(location.search)[1] || '';
            url = prefix + '/sshproxy/' + host + '/' + port + '/' + user + '/' + passwd;
            autoConnect = /autoConnect=([^?&]+)/.exec(location.search)[1];
        } else {
            url = prefix + '/terminal/123?s=r8zYvjxn67mKa3aciJnV/LeyZ2g8m1Rdm2kpGg3e9185oOVwFX+Rzq9E5qJsshVb';
        }

        function connect() {
            if (!window.WebSocket) {
                alert("Unsupported WebSocket");
                return;
            }

            var socket = new WebSocket(url);
            socket.onopen = function () {
                var term = new Terminal({
                    cols: 80,
                    rows: 24,
                    screenKeys: true,
                    useStyle: false
                });

                term.on('title', function (title) {
                    document.title = title;
                });

                term.on('data', function (data) {
                    socket.send(data);
                });

                // term.open(document.body);
                term.open();

                socket.onmessage = function (evt) {
                    term.write(evt.data);
                }

                socket.onclose = function (evt) {
                    showLoader();
                    term.destroy();
                }

                var resize = function () {
                    fixSize(term, socket);
                };

                // Chrome, windows , iframe 100% 没有办法获取焦点
                Terminal.on(term.element, "mousedown", function () {
                    window.focus();
                });

                hideLoader();
                resize();
                setTimeout(resize, 1e3);
                window.onresize = resize;

            }
        }

        function fixSize(term, socket) {
            var cols = document.body.clientWidth / term.element.offsetWidth,
                rows = document.body.clientHeight / term.element.offsetHeight;
            cols = cols * term.cols | 0;
            rows = rows * term.rows | 0;
            term.resize(cols, rows);
            socket.send('resize:' + cols + ',' + rows);
            console && typeof(console.log) === 'function' && console.log('resize: ' + cols + ',' + rows);
        }

        function hideLoader() {
            document.getElementById('loader').style.display = 'none';
        }

        function showLoader() {
            document.getElementById('loader').style.display = 'block';
        }

        function onFocused() {
            document.body.className = 'focused'
        }

        function onBlured() {
            document.body.className = ''
        }

        document.getElementById("loader").onclick = connect;
        window.onblur = onBlured;
        window.onfocus = onFocused;
        autoConnect && connect();
    }).call();
    // ]]>
</script>
</body>
</html>
